<template>
  <div class="home">
    <el-row :gutter="10">
      <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12"
        ><div class="grid-content bg-purple">
          <el-card class="home-card">
            <div slot="header" class="clearfix">
              <span>应用概览</span>
            </div>
            <div class="use-box">
              <el-row :gutter="8">
                <el-col :span="6">
                  <div class="use-item">
                    <p class="use-text">
                      <span>网关总数</span>
                      <span>在线</span>
                      <span>离线</span>
                    </p>
                    <div class="use-number">
                      <b>{{ GWObj.gw_total || 0 }}</b>
                      <b style="color: #329e32">{{ GWObj.gw_online || 0 }}</b>
                      <b style="color: red">{{ GWObj.gw_offline || 0 }}</b>
                    </div>
                  </div>
                </el-col>
                <el-col :span="6">
                  <div class="use-item">
                    <p class="use-text">
                      <span>终端数量</span>
                    </p>
                    <div class="use-number">
                      <b>{{ projObj.teCount }}</b>
                    </div>
                  </div>
                </el-col>
                <el-col :span="6">
                  <div class="use-item">
                    <p class="use-text">
                      <span>遥信点数</span>
                    </p>
                    <div class="use-number">
                      <b>{{ projObj.yxCount }}</b>
                    </div>
                  </div>
                </el-col>
                <el-col :span="6">
                  <div class="use-item">
                    <p class="use-text">
                      <span>遥测点数</span>
                    </p>
                    <div class="use-number">
                      <b>{{ projObj.ycCount }}</b>
                    </div>
                  </div>
                </el-col>
                <el-col :span="6">
                  <div class="use-item">
                    <p class="use-text">
                      <span>遥脉点数</span>
                    </p>
                    <div class="use-number">
                      <b>{{ projObj.ymCount }}</b>
                    </div>
                  </div>
                </el-col>
                <el-col :span="6">
                  <div class="use-item">
                    <p class="use-text">
                      <span>遥控点数</span>
                    </p>
                    <div class="use-number">
                      <b>{{ projObj.ykCount }}</b>
                    </div>
                  </div>
                </el-col>
                <el-col :span="6">
                  <div class="use-item">
                    <p class="use-text">
                      <span>遥调点数</span>
                    </p>
                    <div class="use-number">
                      <b>{{ projObj.ytCount }}</b>
                    </div>
                  </div>
                </el-col>
              </el-row>
            </div>
          </el-card>
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12"
        ><div class="grid-content bg-purple">
          <el-card class="home-card">
            <div slot="header" class="clearfix">
              <span>服务器信息</span>
            </div>
            <div class="system-box" style="height: 100%">
              <el-descriptions :column="1" border size="small">
                <el-descriptions-item>
                  <template slot="label">服务器名称 </template>
                  {{ sysObj.computerName }}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template slot="label"> 操作系统 </template>
                  {{ sysObj.osName }}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template slot="label"> 服务器IP </template>
                  {{ sysObj.computerIp }}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template slot="label"> 系统架构 </template>
                  {{ sysObj.osArch }}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template slot="label"> 项目路径 </template>
                  {{ sysObj.userDir }}
                </el-descriptions-item>
              </el-descriptions>
            </div>
          </el-card>
        </div>
      </el-col>
    </el-row>
    <el-row :gutter="10">
      <el-col :xs="24" :sm="24" :md="8" :lg="8" :xl="8"
        ><div class="grid-content bg-purple">
          <el-card class="home-card">
            <div slot="header" class="clearfix">
              <span>内存信息</span>
            </div>
            <div class="echars-box">
              <el-row :gutter="10">
                <el-col :span="6">
                  <div
                    class="pie"
                    ref="pieRef2"
                    style="width: 100%; height: 200px"
                  >
                    <div style="font-size: 14px; margin-top: 50%">
                      <div v-if="memObj.total">内存总量:</div>
                      <b v-if="memObj.total">{{ memObj.total + "GB" }}</b>
                    </div>
                  </div>
                </el-col>
                <el-col :span="18">
                  <div
                    class="pie"
                    ref="pieRef1"
                    style="width: 100%; height: 200px"
                  ></div>
                </el-col>
              </el-row>
            </div>
          </el-card></div
      ></el-col>
      <el-col :xs="24" :sm="24" :md="8" :lg="8" :xl="8"
        ><div class="grid-content bg-purple">
          <el-card class="home-card">
            <div slot="header" class="clearfix">
              <span>CPU信息</span>
            </div>
            <div class="echars-box">
              <el-row :gutter="10">
                <el-col :span="6">
                  <div
                    class="pie"
                    ref="pieRef2"
                    style="width: 100%; height: 200px"
                  >
                    <div style="font-size: 14px; margin-top: 50%">
                      <div v-if="cpuObj.cpuNum">核心数 :</div>
                      <b>{{ cpuObj.cpuNum }}</b>
                    </div>
                  </div>
                </el-col>
                <el-col :span="18">
                  <div
                    class="pie"
                    ref="pieRef2"
                    style="width: 100%; height: 200px"
                  ></div>
                </el-col>
              </el-row>
            </div>
          </el-card></div
      ></el-col>
      <el-col :xs="24" :sm="24" :md="8" :lg="8" :xl="8"
        ><div class="grid-content bg-purple">
          <el-card class="home-card">
            <div slot="header" class="clearfix">
              <span>虚拟机信息</span>
            </div>
            <div class="echars-box">
              <el-descriptions size="small" :column="1" border>
                <el-descriptions-item>
                  <template slot="label">JVM占用的内存总数</template>
                  {{ jvmObj.total + "MB" }}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template slot="label"> JVM最大可用内存总数 </template>
                  {{ jvmObj.max + "MB" }}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template slot="label"> JVM空闲内存 </template>
                  {{ jvmObj.free + "MB" }}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template slot="label"> JDK版本 </template>
                  {{ jvmObj.version }}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template slot="label"> JDK路径 </template>
                  {{ jvmObj.home }}
                </el-descriptions-item>
              </el-descriptions>
            </div>
          </el-card>
        </div></el-col
      >
    </el-row>
    <el-row :gutter="10">
      <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24"
        ><div class="grid-content bg-purple">
          <el-card class="home-card">
            <div slot="header" class="clearfix">
              <span>磁盘状态</span>
            </div>
            <div class="echars-box">
              <yg-table
                :columns="columns"
                :tableList="TableList"
                :height="'200'"
              ></yg-table>
            </div>
          </el-card></div
      ></el-col>
    </el-row>
  </div>
</template>
<script>
import YgTable from "@/components/Table/YgTable.vue";
import { setMessageCallback, publishMsg, removeMessageCallback } from "@/mqtt";

export default {
  name: "Home",
  components: {
    YgTable,
  },
  data() {
    return {
      columns: [
        {
          prop: "dirName",
          label: "盘符路径",
        },
        {
          prop: "sysTypeName",
          label: "盘符类型",
        },
        {
          prop: "typeName",
          label: "文件类型",
        },
        {
          prop: "total",
          label: "总大小",
        },
        {
          prop: "free",
          label: "剩余大小",
        },
        {
          prop: "used",
          label: "已经使用量",
        },
        {
          prop: "usage",
          label: "资源的使用率",
          formatter(row) {
            if (row.hasOwnProperty("usage")) {
              return row.usage + "%";
            } else {
              return "";
            }
          },
        },
      ],
      TableList: [],
      myChart: null,
      myChart1: null,
      sysObj: {
        computerName: "",
        computerIp: "",
        osName: "",
        osArch: "",
        userDir: "",
      },
      cpuObj: {
        cpuNum: "",
        sys: "",
        used: "",
        free: "",
      },
      jvmObj: {
        total: "",
        max: "",
        free: "",
      },
      memObj: {
        total: "",
        used: "",
        free: "",
      },
      projObj: {},
      memArray: [],
      cpuArray: [],
      GWObj: {},
      TEObj: {},
    };
  },
  created() {},
  mounted() {
    setMessageCallback(this.onMessage);
    publishMsg({
      webMsgType: 1000,
      funCode: 3,
    });
    publishMsg({
      webMsgType: 1000,
      funCode: 4,
    });
  },
  watch: {
    cpuObj: {
      deep: true,
      handler(oldval, newval) {
        if (!this.objEqual(oldval, newval)) {
          this.myCharPie();
        }
      },
    },
    memObj: {
      deep: true,
      handler(oldval, newval) {
        if (!this.objEqual(oldval, newval)) {
          this.testLine();
        }
      },
    },
  },
  destroyed() {
    removeMessageCallback(this.onMessage);
  },
  methods: {
    objEqual(a, b) {
      if (Object.keys(a).length !== Object.keys(b).length) {
        return false;
      }
      for (let key in a) {
        if (b[key] === undefined) {
          return false;
        }
        if (typeof b[key] === "function" || typeof a[key] === "function") {
          throw new Error("type error");
        }
        if (typeof b[key] != typeof a[key]) {
          return false;
        }
        if (typeof a[key] === "object") {
          return objEqual(a[key], b[key]);
        }
        if (a[key] !== b[key]) {
          return false;
        }
      }
      return true;
    },
    onMessage(topic, msg) {
      // console.log(topic, msg);
      switch (msg.webMsgType) {
        case 1000:
          switch (msg.funCode) {
            case 3:
              if (!msg.data) {
                return this.$message.error(msg.retMsg);
              }
              this.sysObj = msg.data.sys;
              this.TableList = msg.data.disk;
              this.projObj = msg.data.proj;
              this.jvmObj = msg.data.jvm;
              this.cpuObj = msg.data.cpu;
              this.memObj = msg.data.mem;
              break;
            case 4:
              if (!msg.data) {
                return this.$message.error(msg.retMsg);
              }
              this.GWObj = msg.data;
              // console.log(this.GWObj);
              break;
          }
          break;
        case 1007:
          switch (msg.funCode) {
            case 6:
              if (msg.retCode != 0) {
                return this.$message.error(msg.retMsg);
              }
              this.TEObj = msg.data;
              // console.log(this.TEObj);
              break;
          }
        default:
          break;
      }
    },
    isClose(item) {
      item.show = !item.show;
    },
    testLine() {
      if (
        this.myChart != null &&
        this.myChart != "" &&
        this.myChart != undefined
      ) {
        this.myChart.dispose(); //销毁
      }
      this.myChart = this.$echarts.init(this.$refs.pieRef1);
      var options = {
        title: {
          text: "内存占比",
          x: "center",
          y: "bottom",
          textStyle: {
            color: "#8ECBFF",
          },
        },
        tooltip: {
          trigger: "item",
          formatter: "{b}:{c}" + "GB" + "({d}%)",
        },
        legend: {
          top: "0%",
          left: "center",
        },
        color: ["#5c7bd9", "#329e32"],
        series: [
          {
            name: "Access From",
            type: "pie",
            radius: ["70%", "20%"],
            avoidLabelOverlap: false,
            label: {
              show: true,
              // position: "center",
              formatter: "{d}%",
            },
            emphasis: {
              label: {
                show: true,
                fontSize: "12",
                fontWeight: "bold",
              },
            },
            labelLine: {
              show: false,
            },
            data: [
              { value: this.memObj.used, name: "已用内存" },
              { value: this.memObj.free, name: "剩余内存" },
            ],
            // data:this.memArray
          },
        ],
      };
      this.myChart.setOption(options);
      window.addEventListener("resize", () => {
        this.myChart.resize();
      });
    },
    myCharPie() {
      if (
        this.myChart1 != null &&
        this.myChart1 != "" &&
        this.myChart1 != undefined
      ) {
        this.myChart1.dispose(); //销毁
      }
      this.myChart1 = this.$echarts.init(this.$refs.pieRef2);
      var options = {
        title: {
          text: "CPU占比",
          x: "center",
          y: "bottom",
          textStyle: {
            color: "#8ECBFF",
          },
        },
        tooltip: {
          trigger: "item",
          formatter: "{b}:{c}({d}%)",
        },
        legend: {
          top: "0%",
          left: "center",
        },
        color: ["#5c7bd9", "#ff915a", "#329e32"],
        series: [
          {
            name: "Access From",
            type: "pie",
            radius: ["70%", "20%"],
            // avoidLabelOverlap: false,
            label: {
              show: true,
              // position: "center",
              formatter: "{d}%",
              // formatter: "{b}:{c}({d}%)",
            },
            emphasis: {
              label: {
                show: true,
                fontSize: "12",
                fontWeight: "bold",
              },
            },
            labelLine: {
              show: false,
            },
            data: [
              { value: this.cpuObj.sys, name: "系统使用率" },
              { value: this.cpuObj.used, name: "用户使用率" },
              { value: this.cpuObj.free, name: "当前空闲率" },
            ],
            // data:this.cpuArray
          },
        ],
      };
      this.myChart1.setOption(options);
      window.addEventListener("resize", () => {
        this.myChart1.resize();
      });
    },
  },
};
</script>
<style scoped lang="scss">
.home {
  margin-top: -10px;
}
::v-deep .el-col {
  height: 100%;
}
.bg-purple-dark {
  background: pink;
}
.bg-purple {
  background: red;
  border-radius: 5px;
}
.bg-purple-light {
  background: green;
}
.grid-content {
  min-height: 36px;
  margin-top: 10px;
}

::v-deep .home-card {
  // margin-top: -10px;
  height: 264px;
  .el-card__header {
    padding: 5px 15px;
  }
}
.app-box {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  .app-item {
    // background: pink;
    // justify-items: flex-end;
    .img {
      width: 60px;
      height: 60px;
      background-color: rgba(235, 235, 235, 1);
      img {
        // width: 100%;
        // height: 100%;
        margin: 10px;
      }
    }
    .text {
      text-align: center;
      font-weight: 600;
    }
  }
}
.use-box {
  width: 100%;
  margin-top: -7px;
  .use-item {
    width: 100%;
    background-color: rgba(235, 235, 235, 1);
    padding: 10px 0px;
    // margin-left: 10px;
    margin-top: 10px;
    .use-text {
      margin: 0;
      // margin-left: 10px;
      display: flex;
      justify-content: space-around;
      color: rgba(145, 142, 142, 0.959);
      span {
        // margin: 0px 10px;
      }
    }
    .use-number {
      // margin-left: 10px;
      display: flex;
      justify-content: space-around;
      b {
        font-size: 27px;
        color: #299cf0;
      }
      .el-progress {
        margin-top: 15px;
      }
    }
  }
}
.echars-box {
  height: 200px;
}
</style>
